﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeterServiceClientApp.UC;
using EntityAndDomainLibrary.EntityModel;
using Microsoft.Reporting.WinForms;
using EntityAndDomainLibrary.DomainModel;
using Log4NetLibrary;

namespace MeterServiceClientApp
{
    public partial class frmReport : Form
    {

        #region Private Variables
        DataBaseAccess dbAccess = new DataBaseAccess();
        ILogService logService;
        
        #endregion

        public frmReport()
        {
            InitializeComponent();
            ucTrendingReport uc = new ucTrendingReport();

            try
            {
                log4net.Config.XmlConfigurator.Configure();
                logService = new FileLogService(typeof(ucTrendingReport));

               
                LoadComboBox();
                LoadReport(DateTime.Now, DateTime.Now, 1);
                
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
           

           
        }

        private void frmReport_Load(object sender, EventArgs e)
        {

            this.rptViewer.RefreshReport();
        }


        private void LoadComboBox()
        {
            try
            {
                cmbMeter.DataSource = dbAccess.GetAllMeterpoint();
                cmbMeter.ValueMember = "meterpointid";

                cmbMeter.DisplayMember = "name";
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                LoadReport(dtpStartDate.Value, dtpEndDate.Value, Convert.ToInt32(cmbMeter.SelectedValue));
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void LoadReport(DateTime startDate, DateTime enddate, int meterpt)
        {

            try
            {
                IList<ReportData> ds = dbAccess.GetReportData(startDate, enddate, meterpt);
                ReportDataSource reportDataSource = new ReportDataSource();
                // Must match the DataSource in the RDLC
                reportDataSource.Name = "ds_trendingdataset";
                reportDataSource.Value = ds;
                rptViewer.LocalReport.DataSources.Clear();
                rptViewer.LocalReport.DataSources.Add(reportDataSource);

                //rptViewer.da();

                rptViewer.LocalReport.ReportEmbeddedResource =
                    "MeterServiceClientApp.UC.Reports.Copy of rpt_Trending.rdlc";
                rptViewer.RefreshReport();
                //"MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc";
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            LoadReport(dtpStartDate.Value, dtpEndDate.Value, Convert.ToInt32(cmbMeter.SelectedValue));
        }
    }
}
